package org.example;

import com.alibaba.druid.sql.SQLUtils;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.net.URL;
import java.util.Arrays;
import java.util.List;

public class AppLaunch extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();


        TabPane tabPane = new TabPane();
        tabPane.setPrefHeight(768.0);
        tabPane.setPrefWidth(1024.0);
        //给tb1设置水平布局面板
        Tab tb1 = new Tab("Sql Parma Tool");

        final TextArea textSql = new TextArea();
        textSql.setPrefColumnCount(85);
        textSql.setPrefHeight(120);
        textSql.setPrefWidth(1024);
        //设置提示
        Tooltip tip1 = new Tooltip("输入带?的SQL文本");
        textSql.setTooltip(tip1);
//        设置文本框背景提示，需要去除焦点，并去除文本框文字
        textSql.setPromptText("例如： select id,name from table1 where id = ? and name = ?");

        final TextArea textParma = new TextArea();
        textParma.setPrefColumnCount(85);
        textParma.setPrefHeight(120);
        textParma.setPrefWidth(1024);
        //设置提示
        Tooltip tip2 = new Tooltip("输入绑定变量字符串");
        textParma.setTooltip(tip2);
//        设置文本框背景提示，需要去除焦点，并去除文本框文字
        textParma.setPromptText("123(Integer), myname(String)");

        final Button getResultButton = new Button("获取替换结果");
        getResultButton.setStyle("-fx-background-color: slateblue; -fx-text-fill: white;");
        //diffButton.getStyleClass().setAll("btn", "btn-default");


        final TextArea textSqlResult = new TextArea();
        textSqlResult.setPrefColumnCount(85);
        textSqlResult.setPrefHeight(280);
        textSqlResult.setPrefWidth(1024);

        final Button clearDiffButton = new Button("清空输入");
        clearDiffButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                textSql.clear();
                textParma.clear();
                textSqlResult.clear();
            }
        });

        getResultButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                String sql = textSql.getText();
                String parameters=textParma.getText();
                List<String> sqlArr = Arrays.asList(sql.split("\\?"));
                List<String> parametersArr = Arrays.asList(parameters.split(", "));
                int i=0;
                String resultSql = "";
                for(String item: sqlArr){
                    if(i>parametersArr.size()-1){
                        break;
                    }
                    resultSql = resultSql + ""+ item;
                    String parameterStr = parametersArr.get(i);
                    System.out.println("parameterStr: " + parameterStr);
                    if(!"null".equals(parameterStr) && parameterStr.split("\\(")[1].startsWith("String")){
                        resultSql = resultSql + "'"+parameterStr.split("\\(")[0] +"'";
                    }else if(!"null".equals(parameterStr) && parameterStr.split("\\(")[1].startsWith("Timestamp")){
                        resultSql = resultSql + "STR_TO_DATE('"+parameterStr.split("\\(")[0] +"','%Y-%m-%d %H:%i:%s')";
                    }else{
                        resultSql = resultSql +parameterStr.split("\\(")[0];
                    }
                    i++;
                }
                String finalSql = resultSql + sqlArr.get(sqlArr.size()-1);
                //System.out.println(resultSql + sqlArr[sqlArr.size()-1]);
                String sqlResult =SQLUtils.format(finalSql, "mysql");
                System.out.println(sqlResult);
                textSqlResult.setText(sqlResult);
            }
        });

        HBox hBox1 = new HBox();
        hBox1.setSpacing(5);
        hBox1.setPadding(new Insets(10, 0, 10, 0));
        hBox1.getChildren().addAll( getResultButton, clearDiffButton);

        VBox vbox0 = new VBox();
        vbox0.setSpacing(5);
        vbox0.setPadding(new Insets(10, 0, 10, 0));
        vbox0.getChildren().addAll(textSql, textParma, hBox1, textSqlResult);

        tb1.setContent(vbox0);
        tb1.setClosable(false);

        tabPane.getTabs().addAll(tb1);

        AnchorPane.setTopAnchor(tabPane,40.0);
        AnchorPane.setLeftAnchor(tabPane,20.0);
        an.getChildren().addAll(tabPane);

        Scene mainScene = new Scene(an);
        // 加载全局CSS
        //mainScene.getStylesheets().add(getClass().getResource("css/modena_dark.css").toExternalForm());
        URL sampleCss = getClass().getResource("/css/light_theme.css");
        mainScene.getStylesheets().addAll(sampleCss.toString());

        primaryStage.setScene(mainScene);
        //primaryStage.getScene().getStylesheets().add(ReadJSONFileUtil.getFileText("css/modena_dark.css"));
        primaryStage.setTitle("SqlParmaTool - by liuyouzhang");
        primaryStage.setWidth(1200);
        primaryStage.setHeight(900);
        //设置窗口不可拉伸
        primaryStage.setResizable(false);
        primaryStage.show();
    }

    @Override
    public void stop() {
        System.out.println("程序停止");
        // 停止程序的逻辑
        // 释放资源
        // 保存数据
        // ...
        System.exit(0);
    }

    public static void main(String[] args){
        Application.launch();
    }
}
